package com.huluobo.a2112mvvmdouyindemo.login;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.huluobo.mvvm_base.bean.UserBeanResult;
import com.huluobo.mvvm_base.net.Const;
import com.huluobo.mvvm_base.viewmodel.BaseViewModel;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/3/22.
 */
public class LoginViewModel extends BaseViewModel<LoginRepo> {
    //成功LiveData
    MutableLiveData<UserBeanResult> success = new MutableLiveData<>();

    //失败的LiveData
    MutableLiveData<String> failed = new MutableLiveData<>();

    public void login(String username, String password) {
        //封装参数,把String转换为body
        //生成map,保存键值对
        //所有的键都是在接口文档中的,文档中写什么就是什么
        Map<String, String> map = new HashMap<>();
        map.put(Const.PARAM_USERNAME, username);
        map.put(Const.PARAM_PASSWORD, password);

        //把map转换位一个json
        String json = new Gson().toJson(map);
        //把json转换为body,因为接口中只接收body
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_PARSE), json);
        repo.login(body, success, failed);
    }

    @Override
    protected LoginRepo createRepo() {
        return new LoginRepo();
    }
}
